package shudu;
/**
 * @author 田野
 * @version 创建时间：2014年1月2日 下午1:32:59
 * 
 */
public class Chessboard {
	 
	protected Cell[][] cells;
	
	protected Chessboard(Cell[][] cells) {
		this.cells = cells;
	}

	public Chessboard(int[][] value) {
		if(value.length < 1) {
			throw new IllegalArgumentException("int array error.");
		}
		cells = new Cell[value.length][];
		for(int i = 0; i < value.length ; i ++) {
			cells[i] = new Cell[value[i].length];
			for(int j = 0; j < value[i].length ; j++) {
				cells[i][j] = new Cell();
				cells[i][j].x = i;
				cells[i][j].y = j;
				cells[i][j].number = value[i][j] == 0 ? null : value[i][j];
			}
		}
	}
	
	public Chessboard(Integer[][] value) {
		if(value.length < 1) {
			throw new IllegalArgumentException("int array error.");
		}
		cells = new Cell[value.length][];
		for(int i = 0; i < value.length ; i ++) {
			cells[i] = new Cell[value[i].length];
			for(int j = 0; j < value[i].length ; j++) {
				cells[i][j] = new Cell();
				cells[i][j].x = i;
				cells[i][j].y = j;
				cells[i][j].number = value[i][j] == 0 ? null : value[i][j];
			}
		}
	}
	
	public Chessboard(String[][] value) {
		if(value.length < 1) {
			throw new IllegalArgumentException("int array error.");
		}
		cells = new Cell[value.length][];
		for(int i = 0; i < value.length ; i ++) {
			cells[i] = new Cell[value[i].length];
			for(int j = 0; j < value[i].length ; j++) {
				cells[i][j] = new Cell();
				cells[i][j].x = j;
				cells[i][j].y = i;
				cells[i][j].number = value[i][j] == "0" ? null : Integer.valueOf(value[i][j]);
			}
		}
	}
	/**
	 * @return the width
	 */
	public int getWidth() {
		return cells[0].length;
	}

	/**
	 * @return the height
	 */
	public int getHeight() {
		return cells.length;
	}

	public Cell getCell(int x,int y) {
		return cells[x][y];
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		int count = 0;
		for(int i = 0 ; i < cells.length ; i++) {
			if(i%3 == 0) {
				sb.append("----------------------\n");
			}
			for(int j = 0 ; j < cells.length ; j++) {
				if(getCell(i, j).getNumber() != null) {
					count++;
				}
				if (j%3 == 0) {
					sb.append("|");
				}
				sb.append((getCell(i, j).getNumber() == null ? " " : getCell(i, j).getNumber()) + " ");
				if (j == cells.length -1) {
					sb.append("|");
				}
			}
			sb.append("\n");
			if(i == cells.length -1) {
				sb.append("----------------------\n");
			}
		}
		sb.append("find count " + count);
		return sb.toString();
	}
}

class Cell {
	Integer number;
	int x;
	int y;
	
	/**
	 * @return the x
	 */
	public int getX() {
		return x;
	}

	/**
	 * @param x the x to set
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * @return the y
	 */
	public int getY() {
		return y;
	}

	/**
	 * @param y the y to set
	 */
	public void setY(int y) {
		this.y = y;
	}

	/**
	 * @param number the number to set
	 */
	public void setNumber(Integer number) {
		if(x == 2 && y == 8) {
			System.out.println("aaa");
		}
		this.number = number;
	}

	public Integer getNumber() {
		return number;
	}
}
